<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title><s:text name="loginPage"/></title>
    <script type="text/javascript">
        function register(){
            targetForm = document.forms[0];
//            targetForm.action = "login!register";
            targetForm.action = "register";
        }
        function login() {
            targetForm = document.forms[0];
            targetForm.action = "login";
        }
    </script>
</head>
<body>
<p>Convention: login-index.jsp</p>
<span style="color: red">${requestScope.get("tip")}</span>
<%--<s:form>--%>
    <%--<s:textfield name="username" key="user"/>--%>
    <%--<s:textfield name="password" key="pass"/>--%>
    <%--<s:submit key="login" onclick="login()"/>--%>
    <%--<s:submit key="register" onclick="register()"/>--%>
<%--</s:form>--%>
<!-- 视图界面国际化 -->
<s:i18n name="login">
<form method="post" action="">
    <label for="username"><s:text name="user"/></label><input type="text" name="user.name" id="username"><br>
    <label for="password"><s:text name="pass"/></label><input type="text" name="user.pass" id="password"><br>
    <input type="submit" onclick="login()" value='<s:text name="login"/>'>&nbsp;&nbsp;
    <input type="submit" onclick="register()" value='<s:text name="register"/>'>
    <!--防止重复提交,name=struts.token-->
    <%--<s:token/>--%>
</form>
</s:i18n>
<hr>

<s:form action="login2" method="POST" label="struts类型转换map=>对象">
    <s:textfield name="users['one'].name" labelposition="left" label="firstUser"/>
    <s:textfield name="users['one'].pass" labelposition="left" label="firstPass"/>
    <s:textfield name="users['two'].name" labelposition="left" label="secondUser"/>
    <s:textfield name="users['two'].pass" labelposition="left" label="secondPass"/>
    <s:submit />
</s:form>
<hr>

<form action="redirect.action" method="post">
    <label for="targetPage">输入目标页面: </label><input type="text" name="targetPage" id="targetPage"><br>
    <input type="submit" value='转入'>
</form>
<hr>

<h3>struts2标签的使用</h3>
<!--设置变量-->
<s:set var="age" value="25"/>
<p>if/elseif/else标签：&nbsp;
    <s:if test="#age>60">
        老年人
    </s:if>
    <s:elseif test="#age>35">
        中年人
    </s:elseif>
    <s:elseif test="#age>15">
        青年人
    </s:elseif>
    <s:else>
        少年
    </s:else>
</p>

<p>放入StackContext的var都可以用request对象取出\${requestScope.age} :${requestScope.age}</p>
<p>也可以直接EL访问\${age}: ${age}</p>

<p>iterator标签: </p>
<%--<P>s:append拼接多个s:param，将新集合放入到StackContext中</P>--%>
<%--<s:append var="langList">--%>
    <%--<s:param value="{'java','scala'}"/>--%>
    <%--<s:param value="{'C#','C++','C'}"/>--%>
<%--</s:append>--%>

<!-- s:generator将字符串转化为Iterator集合-->
<!-- s:subset获取列表的子集，放入到pageContext中 -->
<!--设置bean-->
<s:bean name="bean.MyComparator" var="myComparator"/>
<table border="1">
    <tr>
        <th>No</th>
        <th>Language</th>
    </tr>
    <s:generator separator="," val="'java,scala,python,c#'" var="lang">
        <s:subset source="#lang" count="2" start="1" var="langSub">
            <s:sort comparator="#myComparator" var="langSubSorted">
                <s:iterator status="st" value="#attr.langSubSorted">
                    <tr <s:if test="#st.Odd">style="background-color: #bbbbbb"</s:if>>
                        <td><s:property value="#st.count"/></td>
                        <td><s:property/></td>
                    </tr>
                </s:iterator>
            </s:sort>
        </s:subset>
    </s:generator>
</table>
<p>s:generator将字符串分割成list在设置var后放入到StackContext中\${requestScope.lang}: ${requestScope.lang}</p>
<p>s:subset获得的iterator对象设置var后放入到pageContext中\${pageScope.langSub}: ${pageScope.langSub}</p>
<p>s:sort排序后的iterator对象设置var后放入到pageContext中\${pageScope.langSubSorted}: ${pageScope.langSubSorted}</p>
<%--<p>迭代map</p>--%>
<%--<table border="1">--%>
    <%--<tr>--%>
        <%--<th>书名</th>--%>
        <%--<th>作者</th>--%>
    <%--</tr>--%>
    <%--<s:iterator var="books" status="st" value="#{'Java':'A','Scala':'B','Python':'C'}">--%>
        <%--<tr <s:if test="#st.odd"> style="background-color: #bbbbbb" </s:if>>--%>
            <%--<td><s:property value="key"/></td>--%>
            <%--<td><s:property value="value"/></td>--%>
        <%--</tr>--%>
    <%--</s:iterator>--%>
<%--</table>--%>
<hr>

<%--<% request.setAttribute("tip","检测是否传递了属性");%>--%>
<%--使用&lt;s:action/&gt;标签调用第一个Action，结果如下：--%>
<%--<s:action name="tag" namespace="/" executeResult="true"/>--%>
<%--<hr>--%>
<%--使用&lt;s:action/&gt;标签调用第一个Action，结果如下：--%>
<%--<s:action name="tag2" namespace="/" executeResult="true" ignoreContextParams="true"/>--%>
<%--<hr>--%>

<s:bean name="bean.Person" var="p">
    <s:param name="name" value="'zz'"/>
    <s:param name="age" value="17"/>
    <p>
        Person类实例的属性：<s:property value="name"/>;<s:property value="age"/>
    </p>
</s:bean>
<s:bean name="java.util.Date" var="now"/>
<p>nice=false且format="dd/MM/yyyy": <s:date name="#now" format="dd/MM/yyyy"/></p>
<p>nice=true: <s:date name="#now" nice="true"/></p>
<p>使用date默认设置: <s:date name="#now" format="dd/MM/yyyy"/></p>
<s:date name="#now" format="dd/MM/yyyy" var="nowDate"/>
<p>设置了date的var属性: ${requestScope.nowDate},<s:property value="#nowDate"/></p>
<s:push value="#p">
    <P>使用s:push将对象压到栈顶，方便访问: <s:property value="name"/></P>
</s:push>
<hr>
<%--<s:include value="login-success.jsp">--%>
    <%--<s:param name="author" value="'zake'"/>--%>
<%--</s:include>--%>

<!--测试Struts的UI标签-->
<s:set var="map" value="#{'Java':'2008.9','Scala':'2008.12','Python':'2014.1'}"/>
<s:form>
    <s:checkboxlist name="a" label="选择喜欢的图书" labelposition="top" list="{'Java','Scala','Python'}"/>
    <s:checkboxlist name="b" label="选择出版日期" labelposition="top"
                    list="#map" listKey="key" listValue="value"/>
    <s:bean name="bean.BookService" var="bs"/>
    <s:checkboxlist name="c" list="#bs.books" label="选择喜欢的书"
                    labelposition="top" listKey="author" listValue="name"/>
    <s:radio name="d" list="#bs.books" listKey="name" listValue="author"
             label="选择最喜欢的作者" labelposition="top"/>
    <s:select name="e" list="#bs.books" listValue="name" label="select books" labelposition="top" multiple="true"/>
    <s:select name="f" list="#bs.books" listValue="author" label="select a author" labelposition="top"/>
    <s:select name="g" list="#bs.books" label="optgroup标签示范" labelposition="top" multiple="true" listValue="name">
        <s:optgroup list="#bs.books" label="alphabets" listValue="author" listKey="name"/>
    </s:select>
    <s:updownselect name="h" list="#bs.books" label="updownselect示例" labelposition="top" emptyOption="true"
                    moveUpLabel="up" moveDownLabel="down" selectAllLabel="all" listValue="name" multiple="true"/>
    <s:doubleselect name="i" doubleList="#map[top]" doubleName="j" list="#map.keySet()"
                    label="doubleSelect示例" labelposition="top"/>
    <s:optiontransferselect name="k" doubleList="#map.values()" doubleName="l" list="#map.keySet()"
                            label="optiontransferselect示例" headerValue="KeySet" doubleHeaderValue="Values"
                            headerKey="k" doubleHeaderKey="v"/>
</s:form>
<%--<s:iterator value="#bs.books">--%>
    <%--<s:property value="author"/>--%>
    <%--<s:property value="name"/>--%>
<%--</s:iterator>--%>
</body>
</html>
